package com.crawler.util;

public class Singleton {
	//单例模式的构造方法必须是私有的,方式外部new对象
	private Singleton(){
	}
	//内部类是延迟加载,所以不会再一开始的时候就加载到内存中,只有第一次使用的时候才加载
	static class SingleHolder{
		public static Singleton singleton = new Singleton();
	}
	public static Singleton getInstance(){
		return SingleHolder.singleton;
	}
	
	public static void main(String[] args) {
		for (int i = 0; i < 100; i++) {
			Test t = new Test(i);
			Thread td = new Thread(t);
			td.start();
		}
	}
}
class Test implements Runnable{
	int i;
	public Test(int i) {
		this.i = i;
	}
	@Override 
	public void run() {
		System.out.println("第"+i+"个的hashCode是:"+Singleton.getInstance().hashCode()+"线程数是"+Thread.currentThread().getName());		
	}
}


